﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// 钢线出入库查询
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class StockInOutInfoController : ControllerBase
    {
        private readonly ILogger<WMSStockInOutInfoRequestDto> _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string con2;


        public StockInOutInfoController(ILogger<WMSStockInOutInfoRequestDto> logger,IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration)
        {
            _logger = logger;
            _WMSTokenCheckService = wmsTokenCheckService;
             con= _configuration.GetConnectionString("WMSDbConnection_ZH");

            con2 = _configuration.GetConnectionString("DDM_DbConnection_Test");


        }

        /// <summary>
        /// 钢线出入库查询
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostStockInOutInfoSelect([FromBody] WMSStockInOutInfoRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            WMSStockInOutInfoRequestDto result = new WMSStockInOutInfoRequestDto();
            WMSStockInOutInfoResponseDto re = new WMSStockInOutInfoResponseDto();
            string source_no = strparm.source_no;
            string supplier_code = strparm.supplier_code;
            string in_datetime = strparm.in_datetime;
            strSql = new StringBuilder();

            try
            {

                strSql.Append(@"select DISTINCT is_push_no  from wms_gxitem_middle");
                DataTable dt_ddm = DB.Query(strSql.ToString(), con2);
                if (dt_ddm.Rows.Count>0)
                {
                    foreach (DataRow item in dt_ddm.Rows)
                    {
                        strSql = new StringBuilder();
                        strSql.Append(@"select *  from in_ddm_codeno where ccode="+ item["is_push_no"].ToString().Trim() + "");
                        DataTable check_info = DB.Query(strSql.ToString(), con);
                        if (check_info.Rows.Count<=0)
                        {
                            strSql = new StringBuilder();
                            strSql.Append(@"insert into in_ddm_codeno(ccode) values ('" + item["is_push_no"].ToString().Trim() + "')");
                            strSqlList.Add(strSql.ToString());
                        }                  
                    }

                    if (strSqlList.Count > 0)
                    {
                        bool A = DB.ExecuteNoQueryTran(strSqlList, con);
                        if (!A)
                        {
                            strMsg = "插入失败失败,更新数据库失败";
                        }
                    }


                    strSql = new StringBuilder();
                    strSql.Append(@"select * from wms_item_inout_middle where 1=1 and is_push_no not in (select ccode from in_ddm_codeno)");
                    if (source_no.Trim() != "")
                    {
                        strSql.Append(" and source_no='" + source_no.Trim() + "'");
                    }
                    if (supplier_code.Trim() != "")
                    {
                        strSql.Append(" and supplier_code='" + supplier_code.Trim() + "'");
                    }
                    if (in_datetime.Trim() != "")
                    {
                        strSql.Append(" and in_datetime>='" + in_datetime.Trim() + "'");
                    }



                    DataTable dt_barcodecheck = DB.Query(strSql.ToString(), con);
                    if (dt_barcodecheck.Rows.Count <= 0)
                    {
                        strMsg = "获取数据失败，请检查清楚再调用接口";
                    }
                    else
                    {
                        re.data = DB.TableToListModel<WMSStockInOutInfoResponseData>(dt_barcodecheck);
                    }
                }
                else
                {
                    strMsg = "获取低代码数据失败";
                }
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

         


            if (strMsg=="")
            {
                re.status = "成功";
                re.code = "1";
                re.message = "";
            }
            else
            {
                re.status = "失败";
                re.code = "0";
                re.message = strMsg;
            }


            return new JsonResult(re);
        }





    }
}
